package xyz.ddlnt.servicemg.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xyz.ddlnt.commonutil.result.Result;
import xyz.ddlnt.model.dto.CategoryDTO;
import xyz.ddlnt.model.entity.Category;
import xyz.ddlnt.servicemg.service.CategoryService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/4/29 22:17
 */
@Slf4j
@RequestMapping("/category")
@RestController
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 查询类别信息
     * @return
     */
    @GetMapping("getAll")
    public Result<List<Category>> getAllCategory() {
        log.info("查询类别信息...");
        return Result.success(categoryService.list());
    }


    @GetMapping("getAllName")
    public Result<List<CategoryDTO>> getAllCategoryName() {
        log.info("查询类别名称信息...");
        List<CategoryDTO> categoryDTOS = categoryService.getAllCategoryName();
        return Result.success(categoryDTOS);
    }

    @PostMapping("add")
    public Result<Category> addCategory(@RequestParam String categoryName) {
        log.info("添加类别信息...");
        Category category =  categoryService.saveCategory(categoryName);
        return Result.success(category);
    }

    @DeleteMapping("delete")
    public Result<String> deleteCategory(@RequestParam Long id) {
        log.info("删除类别信息...");
        boolean delete = categoryService.removeById(id);
        return delete ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @PutMapping("update")
    public Result<LocalDateTime> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新类别信息...");
        LocalDateTime updateTime = categoryService.updateCategory(categoryDTO);
        return Result.success(updateTime);
    }
}
